package output;

import instance.Instance;
import instance.InstanceEj2;
import instance.InstanceEj3;

import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import problem.ej2.Link;



public class Problem2Output implements Output{
	private static String outputFile;
	private List<Instance> solutions;
	
	
	public Problem2Output(List<Instance> solutions) {
		this.solutions = solutions;
	}
	public Problem2Output(String nombArch){
		
	  outputFile =nombArch;
	}
	
	public void writeTestRend(LinkedList<String> strings_salida) throws Exception {
		/*||CUIDADO||:Usar después de generar el tipo Problem3Output con el constructor que cambia el outputfile*/
		BufferedWriter bw = new BufferedWriter(new PrintWriter(
				Problem2Output.outputFile));
		
		Iterator<String> it_res = strings_salida.iterator();
		while(it_res.hasNext()){
			String st = it_res.next();
			bw.write(st);
			if(it_res.hasNext()) bw.newLine();
		}
		bw.close();
	}
	
	@Override
	public void writeFile(String outputFile) throws Exception {
		// TODO Auto-generated method stub

		Problem2Output.outputFile = outputFile;

		BufferedWriter bufferedWriter = new BufferedWriter(new PrintWriter(
				Problem2Output.outputFile));


		// Escribimos todas las soluciones.
		for (int i = 0; i < solutions.size(); i++) {
			String linea = " ";
			// Por cada soluci�n.
			Instance solution =  solutions.get(i);

			printSolution(bufferedWriter, solution,linea);
		}

		bufferedWriter.close();

	}
	// Imprime una soluci�n en el archivo de salida.
	private void printSolution(BufferedWriter bw, Instance solution, String linea) throws Exception {
		for(int i=0; i< ((InstanceEj2) solution).size();i++){
			
		Link inst =((InstanceEj2) solution).get(i);
		
		if (linea == ""){
				linea = String.valueOf((inst).getLoc1());
				linea =linea.concat(" ");
				linea =linea.concat(String.valueOf((inst).getLoc2()));
				linea =linea.concat(" ");
				linea =linea.concat(String.valueOf((inst).getCosto()));				
		  }else{
			linea = linea.concat(";");		
			linea = linea.concat(String.valueOf(inst.getLoc1()));
			linea =linea.concat(" ");
			linea =linea.concat(String.valueOf(inst.getLoc2()));
			linea =linea.concat(" ");
			linea =linea.concat(String.valueOf((inst).getCosto()));
	      }

	} 		bw.write(linea);
	        bw.newLine();
	
	}
	public static void deListaAArchivo(LinkedList<LinkedList<Link> > lista, String nombArch)throws Exception{
		BufferedWriter bw = new BufferedWriter(new PrintWriter(nombArch));
		
		Iterator<LinkedList<Link> > it = lista.iterator();
		while(it.hasNext()){
			LinkedList<Link> inst = it.next();
			
	        String vector = "";
	        Iterator<Link> it_inst = inst.iterator();
			while(it_inst.hasNext()){
				Link l = it_inst.next();
				vector = vector.concat(String.valueOf(l.getLoc1()) + " " + String.valueOf(l.getLoc2()) + " " + String.valueOf(l.getCosto()));
				if(it_inst.hasNext())vector = vector.concat(";");
				
			}
			bw.write(vector);
			if(it.hasNext())bw.newLine();	        
		}    

	    bw.close();
	}
}

